package com.employeemanage.controller;

import com.employeemanage.domain.bean.TblProduct;
import com.employeemanage.domain.resp.PageResult;
import com.employeemanage.domain.resp.Result;
import com.employeemanage.domain.vo.ProductAddVo;
import com.employeemanage.domain.vo.ProductListVo;
import com.employeemanage.domain.vo.ProductUpdateVo;
import com.employeemanage.service.TblProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品管理")
@Validated
public class TblProductController {

    @Autowired
    private TblProductService productService;

    /**
     * 获取商品列表
     * @param productListVo 查询条件
     * @return 商品列表
     */
    @PostMapping("/list")
    @ApiOperation("获取商品列表")
    public Result<PageResult<TblProduct>> getProductList(
            @RequestBody(required = false) ProductListVo productListVo) {
        try {
            if (productListVo == null) {
                productListVo = new ProductListVo();
            }
            PageResult<TblProduct> result = productService.getProductList(productListVo);
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail("获取商品列表失败：" + e.getMessage());
        }
    }

    /**
     * 新增商品
     * @param productAddVo 商品信息
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增商品")
    public Result<Map<String, Object>> addProduct(@Valid @RequestBody ProductAddVo productAddVo) {
        try {
            Long productId = productService.addProduct(productAddVo);
            Map<String, Object> data = new HashMap<>();
            data.put("id", productId);
            return Result.success(data);
        } catch (Exception e) {
            return Result.fail("新增商品失败：" + e.getMessage());
        }
    }

    /**
     * 更新商品
     * @param productUpdateVo 商品信息
     * @return 更新结果
     */
    @PostMapping("/update")
    @ApiOperation("更新商品")
    public Result<String> updateProduct(@Valid @RequestBody ProductUpdateVo productUpdateVo) {
        try {
            boolean success = productService.updateProduct(productUpdateVo);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.fail("商品不存在");
            }
        } catch (Exception e) {
            return Result.fail("更新商品失败：" + e.getMessage());
        }
    }

    /**
     * 删除商品
     * @param request 包含商品ID的请求
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ApiOperation("删除商品")
    public Result<String> deleteProduct(@RequestBody Map<String, Long> request) {
        try {
            Long id = request.get("id");
            if (id == null) {
                return Result.fail("商品ID不能为空");
            }
            
            boolean success = productService.deleteProduct(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.fail("商品不存在");
            }
        } catch (Exception e) {
            return Result.fail("删除商品失败：" + e.getMessage());
        }
    }
}
